//
//  SPCBuyCarViewController.m
//  SmallPigCar
//
//  Created by 张令林 on 2016/11/1.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "SPCBuyCarViewController.h"
#import "UINavigationBar+NavAlpha.h"

@interface SPCBuyCarViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong) NSArray *array;
@property (nonatomic,weak) UIView *selectView;
@property (nonatomic,weak) UIView *quickSelView;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,assign) CGFloat lastContentOffset;
@property (nonatomic,assign) UIPanGestureRecognizer *panGesture;
//添加一个参数，判断是否有图片
@property (nonatomic,weak) UIImageView *imageView;
@property (nonatomic,weak) UIImage *image;
@property (nonatomic,assign) NSInteger flag;


@end

@implementation SPCBuyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //调用初始化方法
    [self setUpUI];
    //刷新数据
    [self loadData];
}

- (void)loadData
{
    NSMutableArray *arrayM = [NSMutableArray new];
    for (int i = 0; i < 16; i++)
    {
        [arrayM addObject:[NSString stringWithFormat:@"%d",i]];
    }
    self.array = arrayM.copy;
    //模拟网络耗时加载数据的模式
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [NSThread sleepForTimeInterval:2];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            //设置截图的视图
            UIImage *image = [self.tableView snapshotImage];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tableView.frame];
            //imageView.image = [UIImage imageWithColor:[UIColor redColor]];
            imageView.image = image;
            imageView.alpha = 0;
            [self.view addSubview:imageView];
            self.imageView = imageView;
        });
    });

}
#pragma mark 初始化方法
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setalphaNavigationWithColor:[UIColor colorWithRed:110 green:203 blue:108]];
 
    //中间的快速选择视图
    UIView *quickSelView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_W, 50)];
    quickSelView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:quickSelView];
    self.quickSelView = quickSelView;
    //下面的tableView视图
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_W, SCREEN_H)];
    tableView.alwaysBounceHorizontal = NO;
    tableView.rowHeight = 80;
    tableView.delegate = self;
    tableView.dataSource = self;
    //tableView.bounces = NO;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    //上面的选择视图
    UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 30)];
    selectView.backgroundColor = [UIColor orangeColor];
    [self.view insertSubview:selectView aboveSubview:self.tableView];
    self.selectView = selectView;

    
}

#pragma mark - 代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    NSLog(@"%lf-----",y);
    if (y == 0) {
        return;
    }
    //获得tableView的Y坐标
    CGFloat tableViewY = self.tableView.frame.origin.y;
    if (tableViewY >= 80)
    {
        self.tableView.bounces = NO;
    }
    else
    {
        self.tableView.bounces = YES;
    }
    //根据拖动方向设置布局
    if (y > self.lastContentOffset)//向上拖动屏幕
    {
        if (tableViewY <= 0)
        {
            
            if (self.tableView.contentOffset.y > 0)
            {
                self.selectView.alpha = 0;
            }
        }else
        {
            if (tableViewY < 80)
            {
                self.tableView.bounces = NO;
            }
            self.imageView.alpha = 1;
            CGFloat imageViewY = tableViewY-y;
            imageViewY = imageViewY < 0 ? 0 : imageViewY;
            if (imageViewY <= 0)
            {
                self.selectView.alpha = 0;
            }
            self.imageView.frame = CGRectMake(0, imageViewY, SCREEN_W, SCREEN_H);
        }
    } else//向下拖动屏幕
    {
        self.selectView.alpha = 1;
        //判断是否滑动到最后,如果滑动到最后就不显示
        CGFloat offSet = SCREEN_H+y-80;
        if ([self.tableView indexPathForRowAtPoint:CGPointMake(0, offSet)].row >= self.array.count-1 || offSet >= self.tableView.rowHeight*(self.array.count-1))
        {
            self.selectView.alpha = 0;
        }
        
        if (tableViewY <= 0)
        {
            if (y < 0)
            {
                self.imageView.alpha = 1;
                CGFloat imageViewY = tableViewY-y;
                imageViewY = imageViewY < 0 ? 0 : imageViewY;
                imageViewY = imageViewY > 80 ? 80 : imageViewY;
                self.imageView.frame = CGRectMake(0, imageViewY, SCREEN_W, SCREEN_H);
            }
        }else
        {
            self.imageView.alpha = 1;
            CGFloat imageViewY = tableViewY-y;
            imageViewY = imageViewY < 0 ? 0 : imageViewY;
            imageViewY = imageViewY > 80 ? 80 : imageViewY;
            self.imageView.frame = CGRectMake(0, imageViewY, SCREEN_W, SCREEN_H);
        }
    }
    self.lastContentOffset = y;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGRect rect = self.imageView.frame;
    if (rect.origin.y > 0)
    {
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        
    }

    self.tableView.frame = rect;
    self.imageView.alpha = 0;
}
#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"leftCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}

@end
